local p = {}
local getArgs = require('Module:Arguments').getArgs
local inArray = require('Module:Util').inArray
local OrderedTable = require('Module:OrderedTable')

local ELEMENT_MAP = require('Module:Util/Config').ELEMENT_MAP
local RARITY_MAP = require('Module:Util/Config').RARITY_MAP
local split_swap = require('Module:Util/SwapImages').split_swap
local weapon_map = require('Module:Util/Weapon')

-----------------------------------------------------------------------------
-- 生成技能表格
-----------------------------------------------------------------------------

function p.support(frame)
	local args = getArgs(frame)
	args.type = '加护技能'
	return p.ability_info(args)
end

function p.charge(frame)
	local args = getArgs(frame)
	args.type = '调律爆发'
	return p.ability_info(args)
end

-----------------------------------------------------------------------------
-- 生成主动技能表格头部
-----------------------------------------------------------------------------

function p.header(frame)
	local args = getArgs(frame)
	local html = mw.html.create():tag('div'):addClass('role-skills-box-title color-frame')

	if args[1] == 'charge' then
		html:tag('div'):addClass('role-skills-box-short'):wikitext('调律爆发')

		html:tag('div'):addClass('role-skills-box-long'):wikitext('调律爆发效果')
	elseif args[1] == 'support' then
		html:tag('div'):addClass('role-skills-box-short'):wikitext('加护技能')

		html:tag('div'):addClass('role-skills-box-long'):wikitext('加护技能效果')
	end
	
	return html
end


-----------------------------------------------------------------------------
-- 生成单个技能
-----------------------------------------------------------------------------

function p.ability_info(args)
	-- 变量
	local skill_icon_div, skill_name_div, skill_name_block, smw_icon, learnlabel
	-- 描述用文本
	local desc_text = ''

	-- 技能图标
	if args.type == '加护技能' then
		if (args.icon or '') ~= '' then
			smw_icon = 'Skill_'..(args.icon or '000_0')..'.png'
		else
			smw_icon = 'core_element_0.png'
		end
	else
		smw_icon = 'Skill_'..(args.icon or '000_0')..'.png'
	end
	
	local html
	if tonumber(args.count) > 0 then 
		html = mw.html.create():tag('div'):addClass('role-skills-box-content background-color-2 role-skills-box-top-line')
	else
		html = mw.html.create():tag('div'):addClass('role-skills-box-content background-color-2')
	end
	local name_div = html:tag('div'):addClass('role-skills-box-short role-skills-box-right-line')
	
	if (args.no_table or '') == '' then
		skill_icon_div = name_div:tag('div')
		if smw_icon ~= 'core_element_0.png' then
			skill_icon_div:wikitext('[[File:'..smw_icon..'|89px|link=]]')
		else
			skill_icon_div:wikitext('[[File:core_element_0.png]]')
		end
		skill_icon_div:done()
		-- 技能名
		if (args.learn or '') ~= '' then
			skill_level_div = name_div:tag('div'):addClass('text')
			if tonumber(args.learn) then
				if tonumber(args.learn) > 100 then
					skill_level_div:tag('span'):addClass('label-item label-color-cyan learn-lv'):cssText('margin-right: 3px;'):wikitext(args.learn..'级')
				elseif tonumber(args.learn) > uncaplevel then
					skill_level_div:tag('span'):addClass('label-item label-color-purple learn-lv'):cssText('margin-right: 3px;'):wikitext(args.learn..'级')
				else
					skill_level_div:tag('span'):addClass('label-item label-color-blue learn-lv'):cssText('margin-right: 3px;'):wikitext(args.learn..'级')
				end
			elseif args.learn == '基能' then
				skill_level_div:tag('span'):addClass('label-item label-color-purple learn-lv'):cssText('margin-right: 3px;'):wikitext(args.learn)
			else
				skill_level_div:tag('span'):addClass('label-item label-color-green learn-lv'):cssText('margin-right: 3px;'):wikitext(args.learn)
			end
			skill_level_div:done()
		end
		skill_name_div = name_div:tag('div'):addClass('skill-name')
		skill_name_div:wikitext(args.name):done()
	end
	
	name_div:done()

	-- 拼合描述
	desc_text = desc_text .. args.desc
	
	html:tag('div'):addClass('role-skills-box-long')
		:tag('div'):addClass('content'):wikitext(desc_text):done()

	-- 存储SMW数据
	args['图标'] = smw_icon
	p.set_smw_data(args)

	return html
end

-----------------------------------------------------------------------------
-- 处理SMW数据
-----------------------------------------------------------------------------

function p.set_smw_data(args)
	-- 导入原始字符串
	local tag_text = args.tag or ''
	args.tag = {}
	if tag_text ~= '' then
		for _, tag in ipairs(mw.text.split(tag_text, ',')) do
			tag = mw.text.trim(tag)
			if tag ~= '' then
				table.insert(args.tag, tag)
			end
		end
	end
	
	local desc_text = args.desc or ''
	local finds, finde, result_text, status_name

	-- 从desc中查询
	
	local find_text = '<span norecord_s>.-<span norecord_e></span>'
	finds, finde = mw.ustring.find(desc_text, find_text)
	while finds do
		desc_text = mw.ustring.sub(desc_text,1,finds-1)..mw.ustring.sub(desc_text,finde+1,-1)
		finds, finde = mw.ustring.find(desc_text, find_text)
	end

	find_text = 'data%-tag="([^"]+)"'
	finds, finde = mw.ustring.find(desc_text, find_text)
	while finds do
		result_text = mw.ustring.sub(desc_text, finds, finde)
		status_name = mw.ustring.gsub(result_text, find_text, '%1')
		table.insert(args.tag, status_name)

		finds, finde = mw.ustring.find(desc_text, find_text, finde)
	end

	
	desc_text=desc_text..'\n\n'
	find_text = '<small class="text%-blue">\n'
	desc_text=string.gsub(desc_text,find_text,'\n<small class="text-blue">\n')
	find_text = '\n%*%*.-[\n]'
	finds, finde = mw.ustring.find(desc_text, find_text)
	while finds do
		desc_text = mw.ustring.sub(desc_text,1,finds-1)..'<ul><li>'..mw.ustring.sub(desc_text,finds+3,finde-1)..'</li></ul>'..mw.ustring.sub(desc_text,finde,-1)
		finds, finde = mw.ustring.find(desc_text, find_text)
	end
	
	find_text = '\n%*.-[\n]'
	finds, finde = mw.ustring.find(desc_text, find_text)
	while finds do
		desc_text = mw.ustring.sub(desc_text,1,finds)..'<ul><li>'..mw.ustring.sub(desc_text,finds+2,finde-1)..'</li></ul>'..mw.ustring.sub(desc_text,finde,-1)
		finds, finde = mw.ustring.find(desc_text, find_text)
	end
	
	find_text = '\n'
	desc_text=string.gsub(desc_text,find_text,'')
	find_text = '</ul><ul>'
	desc_text=string.gsub(desc_text,find_text,'')
	
	--args.desc = desc_text
	
	-- 从cd中查询
	desc_text = args.cd or ''
	find_text = 'data%-tag="([^"]+)"'
	finds, finde = mw.ustring.find(desc_text, find_text)
	while finds do
		result_text = mw.ustring.sub(desc_text, finds, finde)
		status_name = mw.ustring.gsub(result_text, find_text, '%1')
		table.insert(args.tag, status_name)

		finds, finde = mw.ustring.find(desc_text, find_text, finde)
	end

	-- 额外追加数据
	local weapon_id = tonumber(mw.text.split(args.pagename, '/')[2]) or tonumber(args.pagename)
	local name_SMW = args.type..(args.count or '')
	
	args['element'] = 0
	args['rarity'] = 4
	args['ID'] = weapon_id
	args['类型'] = '武器技能'
	return mw.smw.subobject(args, name_SMW)
end

return p
